const mysql = require('mysql')
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'test'
});

connection.connect();
// connection.query('update user set username="666", password="66666" where id=1', function (error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results);
// });
// connection.query('SELECT * from user where id =2', function (error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results);
// });
// connection.query('insert into user values (null,8868,8886)', function (error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results);
// });
// connection.query('delete from  user where id=1', function (error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results);
// });
/**
 * 
 * @param {String} str sql语句
 */
const DBFun = (str, fun) => {
    console.log("=========开始查找sql============");
    try {
        connection.query(str, function (error, results, fields) {
            if (error) {
                console.log("============请注意sql报错============")
                console.log("============请注意sql报错============")
                console.log("============请注意sql报错============")
                console.log("============请注意sql报错============")
                console.log("============请注意sql报错============")
                console.log(error);
                return
            };
            fun(results)
        });
    } catch{

        console.log("请注意sql报错")
        fun({ data: "数据库查询报错" })
    }
}
const DBFunPro = (str) => {
    console.log("=========开始查找sql============");
    try {
        return new Promise((resolve, reject) => {
            connection.query(str, function (error, results, fields) {
                if (error) {
                    console.log("============请注意sql报错============")
                    console.log("============请注意sql报错============")
                    console.log("============请注意sql报错============")
                    console.log("============请注意sql报错============")
                    console.log("============请注意sql报错============")
                    // console.log(error);
                    reject([{ data: "数据库查询报错" }])
                };
                //    console.log('The solution is: ', results);
                resolve(results)
                // console.log("============查找sql成功============");
            });
        })

    } catch{
        console.log("请注意sql报错")
        // fun(['请注意sql报错'])
    }
}
module.exports = {
    DBFun,
    DBFunPro
}